﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import flash.events.*;
    import Transactions.*;
    import Classes.util.*;
    import Display.*;

    public class Stadium extends Decoration {

        public static const STADIUM_LEVELS:String = "stadiumLevels";
        public static const OFFENSE:String = "OFFENSE";
        public static const DEFENSE:String = "DEFENSE";
        public static const SOCCER_STADIUM:String = "soccer";
        public static const BASEBALL_STADIUM:String = "baseball";

        public var TRAINING_XP_GAIN:int;
        private var m_levels:XMLList;
        private var m_stadiumType:String;
        private var m_dialog:StadiumDialog = null;
        private var m_stats:Object = null;
        private var m_isPlaying:Boolean;

        public function Stadium(_arg1:String=null){
            super(_arg1);
            this.m_stadiumType = this.m_item.xml.@stadiumType;
            var _local2:XML = Global.gameSettings.getXML();
            this.m_levels = _local2.stadiumLevels.level;
            this.TRAINING_XP_GAIN = Global.gameSettings.getAttribute("stadiumTrainingXPGain");
            this.m_isPlaying = false;
            var _local3:Stat = new Stat(0);
            this.initializeStats();
        }
        public function initializeStats():void{
            this.m_stats = new Object();
            this.m_stats[Stat.OFFENSE_XP] = new Stat(0);
            this.m_stats[Stat.OFFENSETONEXTLEVEL] = new Stat(Global.gameSettings.getAttribute("stadiumInitialToNextLevel"));
            this.m_stats[Stat.OFFENSE_LEVEL] = new Stat(1);
            this.m_stats[Stat.DEFENSE_XP] = new Stat(0);
            this.m_stats[Stat.DEFENSETONEXTLEVEL] = new Stat(Global.gameSettings.getAttribute("stadiumInitialToNextLevel"));
            this.m_stats[Stat.DEFENSE_LEVEL] = new Stat(1);
            this.m_stats[Stat.ELORANK] = new Stat(Global.gameSettings.getAttribute("stadiumStartingEloRank"));
            this.m_stats[Stat.FANS] = new Stat(0);
            this.m_stats[Stat.STAMINA] = new Stat(Global.gameSettings.getAttribute("stadiumStartingStamina"));
            this.m_stats[Stat.WINS] = new Stat(0);
            this.m_stats[Stat.LOSSES] = new Stat(0);
            this.m_stats[Stat.DRAWS] = new Stat(0);
        }
        override public function loadObject(_arg1:Object):void{
            super.loadObject(_arg1);
            var _local2:Stat = new Stat(0);
            this.m_stats[Stat.OFFENSE_LEVEL].value = _arg1.stats[Stat.OFFENSE_LEVEL].value;
            this.m_stats[Stat.OFFENSE_XP].value = _arg1.stats[Stat.OFFENSE_XP].value;
            this.m_stats[Stat.OFFENSETONEXTLEVEL].value = _arg1.stats[Stat.OFFENSETONEXTLEVEL].value;
            this.m_stats[Stat.DEFENSE_LEVEL].value = _arg1.stats[Stat.DEFENSE_LEVEL].value;
            this.m_stats[Stat.DEFENSE_XP].value = _arg1.stats[Stat.DEFENSE_XP].value;
            this.m_stats[Stat.DEFENSETONEXTLEVEL].value = _arg1.stats[Stat.DEFENSETONEXTLEVEL].value;
            this.m_stats[Stat.ELORANK].value = _arg1.stats[Stat.ELORANK].value;
            this.m_stats[Stat.FANS].value = _arg1.stats[Stat.STAMINA].value;
            this.m_stats[Stat.STAMINA].value = _arg1.stats[Stat.STAMINA].value;
            this.m_stats[Stat.WINS].value = _arg1.stats[Stat.WINS].value;
            this.m_stats[Stat.LOSSES].value = _arg1.stats[Stat.LOSSES].value;
            this.m_stats[Stat.DRAWS].value = _arg1.stats[Stat.DRAWS].value;
        }
        public function get stadiumType():String{
            return (this.m_stadiumType);
        }
        public function get offenseLvl():int{
            return (this.m_stats[Stat.OFFENSE_LEVEL].value);
        }
        public function get defenseLvl():int{
            return (this.m_stats[Stat.DEFENSE_LEVEL].value);
        }
        public function get stats():Object{
            return (this.m_stats);
        }
        public function set playing(_arg1:Boolean):void{
            this.m_isPlaying = _arg1;
        }
        public function get offensePercentage():int{
            return ((((this.m_stats[Stat.OFFENSE_XP].value - this.m_levels[(this.m_stats[Stat.OFFENSE_LEVEL].value - 1)].@requiredXP) / this.m_stats[Stat.OFFENSETONEXTLEVEL].value) * 100));
        }
        public function get defensePercentage():int{
            return ((((this.m_stats[Stat.DEFENSE_XP].value - this.m_levels[(this.m_stats[Stat.DEFENSE_LEVEL].value - 1)].@requiredXP) / this.m_stats[Stat.DEFENSETONEXTLEVEL].value) * 100));
        }
        public function trainOffense():void{
            var _local1:int = this.m_stats[Stat.OFFENSE_LEVEL].value;
            if (_local1 >= Global.gameSettings.getAttribute("stadiumMaxStatLevel")){
                return;
            };
            var _local2:int = this.m_stats[Stat.OFFENSE_XP].value;
            var _local3:int = this.m_stats[Stat.OFFENSETONEXTLEVEL].value;
            _local2 = (_local2 + this.TRAINING_XP_GAIN);
            var _local4:int = _local1;
            while (_local4 < this.m_levels.length()) {
                if (_local2 >= this.m_levels[_local4].@requiredXP){
                    _local1 = (_local1 + 1);
                    _local3 = (this.m_levels[_local1].@requiredXP - this.m_levels[(_local1 - 1)].@requiredXP);
                    this.m_stats[Stat.OFFENSE_LEVEL].value = _local1;
                    this.m_stats[Stat.OFFENSETONEXTLEVEL].value = _local3;
                } else {
                    break;
                };
                _local4++;
            };
            this.m_stats[Stat.OFFENSE_XP].value = _local2;
            GameTransactionManager.addTransaction(new TStadiumTrain(this, OFFENSE), false);
        }
        public function trainDefense():void{
            var _local1:int = this.m_stats[Stat.DEFENSE_LEVEL].value;
            if (_local1 >= Global.gameSettings.getAttribute("stadiumMaxStatLevel")){
                return;
            };
            var _local2:int = this.m_stats[Stat.DEFENSE_XP].value;
            var _local3:int = this.m_stats[Stat.DEFENSETONEXTLEVEL].value;
            _local2 = (_local2 + this.TRAINING_XP_GAIN);
            var _local4:int = _local1;
            while (_local4 < this.m_levels.length()) {
                if (_local2 >= this.m_levels[_local4].@requiredXP){
                    _local1 = (_local1 + 1);
                    _local3 = (this.m_levels[_local1].@requiredXP - this.m_levels[(_local1 - 1)].@requiredXP);
                    this.m_stats[Stat.DEFENSE_LEVEL].value = _local1;
                    this.m_stats[Stat.DEFENSETONEXTLEVEL].value = _local3;
                } else {
                    break;
                };
                _local4++;
            };
            this.m_stats[Stat.DEFENSE_XP].value = _local2;
            GameTransactionManager.addTransaction(new TStadiumTrain(this, DEFENSE), false);
        }
        override public function onPlayAction():void{
            if (m_visitReplayLock > 0){
                return;
            };
            m_actionMode = PLAY_ACTION;
            if ((((this.m_dialog == null)) && ((this.m_isPlaying == false)))){
                this.m_dialog = new StadiumDialog(this, this.m_stadiumType);
                this.m_dialog.addEventListener(Event.CLOSE, this.onDialogClose);
                UI.displayPopup(this.m_dialog);
            };
        }
        private function onDialogClose(_arg1:Event):void{
            this.m_dialog = null;
        }
        override public function isPlacedObjectNonBuilding():Boolean{
            return (false);
        }

    }
}//package Classes 
